<script setup lang="ts">
import { nextTick, reactive, ref } from "vue";
import { useStudentStore } from "../../store";
import { storeToRefs } from "pinia";
import { ElMessage, ElMessageBox } from "element-plus";
import { ApiStudent } from "../../api";

const { classList } = storeToRefs(useStudentStore());
const show = ref<boolean>(false);
const model = reactive<API.Student.Assign>({
  stu_id: null,
  stu_ids: [],
  stu_cls_id: 0,
});
const emits = defineEmits<{
  (name: "save", model: API.Student.Assign): void;
}>();
const cancel = async () => {
  await ElMessageBox.confirm("确定取消？", "提示", { type: "warning" });
  show.value = false;
};
const save = async () => {
  await ApiStudent.assign(model);
  emits("save", model);
  show.value = false;
  ElMessage.success("分配成功");
};
defineExpose({
  begin: (data: number[],stu_cls_id:number|null) => {
    model.stu_cls_id=stu_cls_id ?? 0;
    model.stu_ids = data;
    nextTick(() => {
      show.value = true;
    });
  },
});
</script>

<template>
  <el-dialog title="学生管理--分班" width="500px" v-model="show">
    <el-form ref="form" :model="model" label-width="80px">
      <el-form-item label="班级">
        <el-select v-model="model.stu_cls_id" placeholder="">
          <el-option label="无班级" :value="0"></el-option>
          <el-option
            v-for="item in classList"
            :key="item.cls_id"
            :label="item.cls_name"
            :value="item.cls_id"
          >
          </el-option>
        </el-select>
      </el-form-item>
    </el-form>
    <div slot="footer">
      <el-button @click="cancel">取 消</el-button>
      <el-button type="primary" @click="save">分 配</el-button>
    </div>
  </el-dialog>
</template>

<style scoped></style>
